<template>

  <!-- 返回 -->
  <div class="top">
    <div @click="ret" style="display: flex;cursor:pointer;">
      <el-icon class="el-icon"><Back /></el-icon>
      <p class="bake">返回</p>
    </div>
    <span> | </span>
    <div class="name">{{examInfo.title}}</div>
  </div>

  <div class="stuexam">
    <div class="list">
      <!-- 每道题 -->
      <div class="li" :id="'id' + index" v-for="(item, index) in examInfo.questions" :key="item.id">
        <div class="questiontype">
          <!-- 类型 -->
          <div class="type">
            <div class="num">{{ index + 1 }}</div>
            <div class="typetitle">{{ item.type }}</div>
          </div>
          <!-- 分值 -->
          <span class="score">
            分值：{{ item.scores }}
          </span>
          <!-- <span class="">
            {{ item.studentanswer===item.answer? '' : '得分：0' }}
          </span>
          <span class="" v-if="item.studentanswer===item.answer">
            {{ item.scores }}
          </span> -->
        </div>
        <!-- 题目 -->
        <span class="title" v-html="item.type === '填空题' ? rep(item.title, index) : item.title"></span>
        <!-- 选项 -->
        <div class="optionslist" v-show="item.type === '单选题' || item.type === '多选题'">
          <!-- 选中 单选选项 -->
          <div v-show="item.type === '单选题'" :class="
            item.studentanswer === itemOpt.answerno && item.studentanswer === itemOpt.answerno
              ? 'optionsli wrong'
              : 'optionsli'
          " v-for="(itemOpt, indexOpt) in item.answers" :key="itemOpt.id">
            <div class="left" >
              <!-- 选项  单选题-->
              <div class="opt">{{ arr[indexOpt] }}</div>
              <!-- 答案 -->
              <div class="da">
                <span style="font-size: 13px">{{ itemOpt.content }}</span>

                <span class="correct" v-if="item.answer==itemOpt.answerno && item.studentanswer === itemOpt.answerno">
                  回答正确
                </span>
                <span class="correct2" v-if="item.answer !==itemOpt.answerno && item.studentanswer === itemOpt.answerno">
                  回答错误
                </span>
              </div>

            </div>

          </div>
          <!-- 选项 多选题 -->
          <div v-show="item.type === '多选题'" :class="
            item.studentanswer == null
              ? 'optionsli'
              : item.studentanswer.indexOf(itemOpt.answerno) > -1
                ? 'optionsli wrong'
                : 'optionsli'
          " v-for="(itemOpt, indexOpt) in item.answers" :key="itemOpt.id">
            <div class="left">
              <!-- 选项    多选 checked-->
              <div class="opt checked">{{ arr[indexOpt] }}</div>
              <!-- 答案 -->
              <div class="da">
                <span style="font-size: 13px">{{ itemOpt.content }}</span>

                <span class="correct" v-if="item.answer.includes(itemOpt.answerno) && item.studentanswer.includes(itemOpt.answerno)">
                  回答正确
                </span>
                <span class="correct2"  v-if="!item.answer.includes(itemOpt.answerno) && item.studentanswer.includes(itemOpt.answerno)">
                  回答错误
                </span>
              </div>
            </div>
          </div>

        </div>

        <!-- 判断题 -->
        <div class="optionslist" v-show="item.type === '判断题'">
          <!-- 正确 -->
          <div :class="
            item.studentanswer === '正确' ? 'optionsli wrong' : 'optionsli'
          ">
            <div class="left">
              <div class="opt">
                {{ item.studentanswer === '正确' ? '' : '' }}
              </div>
              <div class="da">
                <span>正确</span>

                <div  v-if="item.answer==item.studentanswer">
                  <span class="correct" v-if="item.answer ==item.studentanswer">
                    回答正确
                  </span>
                  <span class="correct2" v-if="item.answer !==item.studentanswer">
                    回答错误
                  </span>
                </div>

              </div>
            </div>
          </div>
          <!-- 错误 -->
          <div :class="
            item.studentanswer === '错误' ? 'optionsli wrong' : 'optionsli'
          " v-if="item.studentanswer">
            <div class="left">
              <div class="opt">
                {{ item.studentanswer === '错误' ? '' : '' }}
              </div>
              <div class="da">
                <span>错误</span>

                <div  v-if="item.answer !==item.studentanswer">
                  <span class="correct" v-if="item.answer ==item.studentanswer">
                    回答正确
                  </span>
                  <span class="correct2" v-if="item.answer !==item.studentanswer">
                    回答错误
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- 问答题 -->
        <el-input v-show="item.type === '问答题'" v-model="item.studentanswer" rows="5" type="textarea"
          placeholder="请输入问答" />
          <div class="daan">
              正确答案：{{item.answer}}
          </div>
      </div>
    </div>
    <!-- 答题卡 -->
    <div class="answer">
      <div class="sheet">答题卡</div>
      <div class="nums">
        <div class="box"></div>
        <span>正确</span>
        <div class="boxwrong"></div>
        <span>错误</span>
      </div>
      <!-- 题目的序号 -->
      <div class="answerbox" >
        <!-- 已达 wrong box   未答 box -->
        <div @click="location(index)" :class="item.studentanswer ? 'wrong box' : 'box'"
         v-for="(item, index) in examInfo.questions" :key="item.id">
          <div class="">
            {{ index + 1 }}
          </div>
        </div>
      </div>

    </div>
  </div>
</template>

<script lang="ts" setup>
import { Back } from '@element-plus/icons-vue';
import {
  reactive,
  toRefs,
  watchEffect,
  nextTick,
} from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { getForResult } from "../../api/index";
const router=useRouter()

const route = useRoute();
console.log(route.query.id);
const data: any = reactive({
  testid: 0,
  examInfo: {},
  answered: 0,
  arr: [
    'A',
    'B',
    'C',
    'D',
    'E',
    'F',
    'G',
    'H',
    'I',
    'J',
    'K',
    'L',
    'M',
    'N',
    'O',
    'P',
    'Q',
    'R',
    'S',
    'T',
    'U',
    'V',
    'W',
    'X',
    'Y',
    'Z',
  ],
});
const { arr, examInfo, answered } = toRefs(data);


// 返回
const ret = () => {
  router.push({ path: './examresults' });
};


// 获取考试题目（开始考试）
const testStart = async () => {
  const res: any = await getForResult({ testid: route.query.id });
  console.log(res);
  if (res.errCode === 10000) {
    data.examInfo = res.data;
    // 获取填空题答案
    nextTick(() => {
      document.querySelectorAll(".input").forEach((item: any) => {
        item.oninput = function () {
          let _index = this.getAttribute("data-index");
          let vals: any = [];
          document.querySelectorAll(".input" + _index).forEach((itm: any) => {
            if (itm.value) vals.push(itm.value);
          });
          data.examInfo.questions[_index].studentanswer =
            vals.length == 0 ? null : vals.join("|");
        };
      });
    });
  }
};
testStart();



// 定位（瞄点）
const location = (index: number) => {
  let itemTop: any = document.getElementById('id' + index);
  window.scrollTo({ top: itemTop.offsetTop, behavior: 'smooth' });
};



// 替换方法(填空题)
const rep = (str: string, index: number) => {
  return str.replace(
    /\[\]/g,
    `<input data-index="${index}" onpaste="return false;" style="margin:0 2px" class="input input${index}" type="text" />`
  );
};
</script>

<style lang="less" scoped>

.top{
  margin-top: 30px;
  margin-bottom: 25px;
  margin-left: 25px;
  width: 130px;
  display: flex;
  justify-content: space-between;
}
.el-icon{
    margin-top: 3px;
    margin-right: 8px;
  }
.stuexam {
  padding: 0px 50px 0;
  display: flex;
  flex-direction: column;
  background-color: #fafbfd;

  .toptitle {
    font-size: 20px;
    margin-top: 20px;
  }

  // 试题开始
  .list {
    margin-top: 10px;

    .li {
      margin-top: 20px;
      display: flex;
      flex-direction: column;
      width: 1020px;
      padding-bottom: 20px;
      border-top: 1px #f0f0f0 solid;
      padding-top: 20px;

      .questiontype {
        display: flex;
        align-items: center;

        .type {
          height: 20px;
          width: 80px;
          border: 1px #3377f9 solid;
          display: flex;
          border-radius: 5px;
          overflow: hidden;
          font-size: 13px;
        }

        .num {
          width: 30%;
          height: 100%;
          background-color: #3377f9;
          color: #fff;
          display: flex;
          justify-content: center;
          align-items: center;
        }

        .typetitle {
          flex: 1;
          height: 100%;
          background-color: #f1f5fb;
          display: flex;
          justify-content: center;
          align-items: center;
          color: #3377f9;
        }
      }

      .score {
        font-size: 12px;
        color: #aca4ab;
        margin: 0 10px;
      }
    }

    .title {
      font-size: 15px;
      margin: 20px 0 20px;
    }

    .optionslist {
      .optionsli {
        height: 35px;
        width: 1000px;
        padding: 0 10px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-radius: 3px;
        margin-bottom: 10px;
        border: 1px solid #fafbfd;

        .left {
          display: flex;
          align-items: center;

          .opt {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            font-size: 12px;
            border: #d3d4d8 1px solid;
            color: #777f86;
            background-color: #fff;
            margin-right: 5px;
            display: flex;
            justify-content: center;
            align-items: center;
          }

          span {
            font-size: 14px;
          }

          .checked {
            border-radius: 5px;
          }
        }
      }

      .wrong {
        background-color: #f1f5fb;
        border: 1px solid #0089ff;

        .left {
          .opt {
            border: #0089ff 1px solid;
            color: #fff;
            background-color: #0089ff;
          }
        }
      }
    }
  }

  // 试题结束
  // 答题卡开始
  .answer {
    position: fixed;
    right: 0;
    top: 0;
    height: 100vh;
    width: 300px;
    background-color: #fff;

    .sheet {
      font-weight: bold;
      font-size: 20px;
      padding: 25px;
    }

    .nums {
      display: flex;
      padding: 0 25px 25px;
      align-items: center;

      .box {
        width: 16px;
        height: 16px;
        background-color: #008000;
        margin-right: 10px;
      }

      .boxwrong {
        width: 15px;
        height: 15px;
        border: 1px solid #e9e9e9;
        background-color: red;
        margin-right: 10px;
      }

      span {
        margin-right: 10px;
      }
    }

    .answerbox {
      display: flex;
      flex-wrap: wrap;
      padding: 0 20px;

      .box {
        width: 35px;
        height: 35px;
        border: 1px solid #e3e3e3;
        text-align: center;
        line-height: 35px;
        margin-left: 6px;
        margin-bottom: 6px;
        border-radius: 5px;
      }

      .wrong {
        background-color: #f0f8ff;
        color: #aab4fd;
      }
    }

    .bottom {
      padding: 0 20px;
      position: fixed;
      width: 260px;
      bottom: 20px;
      background-color: #fff;

      p {
        font-size: 12px;
        color: #989a99;

        span {
          color: #000;
        }
      }

      .buttons {
        width: 100%;
        font-size: 12px;
        margin-top: 10px;
      }
    }
  }
}

.da{
  width: 969px;
  display: flex;
  justify-content: space-between;
}
.correct{
  color: #008000;
}
.correct2{
  color: red;
}


.daan{
  color: rgb(174, 193, 224);
}

</style>
